import $math from "@src/common/math";
import {yuan} from "@src/common/utils";
import base from "@src/store/base";
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";


Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
      store: base,
      fields: ['themeColor', 'themeTitleColor']
  }],
  properties: {
    store: {
      type: Object
    },
    deliveryType: {
      type: String
    },
    delivery: {
      type: Object
    },
    disable: {
      type: Boolean,
      value: false
    }
  },
  data: {
    prepayTime: null,
    bookings: false,
    tips: [],
    curDelivery: [],
  },
  observers: {
    'delivery': function (delivery) {
      if (delivery && !this.data.disable) {
        this.init();
      }
    }
  },
  methods: {
    init() {
      let curDelivery = null;
      let tips = [];
      let bookings = false
      let prepayTime = '';
      let unitMap = {
        'HOURS': '小时',
        'DAYS': '天',
        'MINUTES': '分钟'
      }
      if (this.data.deliveryType === 'EXPRESS') {
        curDelivery = this.data.delivery.express;
        if (curDelivery.freightType === 0) {
          tips.push("商家包邮")
        } else {
          tips.push("邮费" + yuan(curDelivery.freightFee) + "元");
          if (curDelivery.freeRule === 2) {
            tips.push("满" + yuan(curDelivery.freeBase) + "元免运费");
          } else if (curDelivery.freeRule === 1) {
            tips.push("满" + curDelivery.freeBase + "件免运费");
          }
        }
        bookings = curDelivery.bookingDays > 0;
        if (curDelivery.prepayTime > 0) {
          prepayTime = '最快下单后' + curDelivery.prepayTime + unitMap[curDelivery.prepayTimeUnit] + '发货';
        }
      } else if (this.data.deliveryType === 'SELF_PICKUP') {
        curDelivery = this.data.delivery.selfPickup
        bookings = false;
        if (curDelivery.prepayTime > 0) {
          prepayTime = '最快' + curDelivery.prepayTime + unitMap[curDelivery.prepayTimeUnit] + '可到店取货';
        }
      } else{
        console.info("this.data.delivery", this.data.delivery)
        curDelivery = this.data.delivery.local
        if (!curDelivery) {
          return;
        }
        if (curDelivery.minPrice >= 0) {
          tips.push("订单￥" + yuan(curDelivery.minPrice ) + " 起送");
        }
        if (curDelivery.freightFee >= 0) {
          tips.push("配送费￥" + yuan(curDelivery.freightFee ) + " 起");
        }
        bookings = curDelivery.bookingDays > 0;
        if (curDelivery.prepayTime > 0) {
          prepayTime = '最快下单后' + curDelivery.prepayTime + unitMap[curDelivery.prepayTimeUnit] + '发货';
        }
      }
      this.setData({curDelivery, tips, bookings, prepayTime})
    },
    goLocation(e) {
      let dataObj = e.currentTarget.dataset.item;
      wx.openLocation({
          name: dataObj.name,
          address: dataObj.address,
          latitude: dataObj.latitude,
          longitude: dataObj.longitude,
          scale: 18
      })
  },
  //调起手机拨号功能
  telphone(e) {
      let phone = e.currentTarget.dataset.phone
      console.log(e)
      if (phone) {
          wx.makePhoneCall({
              phoneNumber: e.currentTarget.dataset.phone, //模拟测试
              success: function () {
                  console.log("拨打电话成功！")
              },
              fail: function () {
                  console.log("拨打电话失败！")
              }
          })
      } else {
          wx.showToast({ title: "该店铺暂未设置客服电话", icon: "none" });
      }
  },
  }
});
